import { toast } from "nc-lightapp-front";
import requestApi from "../requestApi";

export default function (props) {
  props.createUIDom(
    {
      pagecode: "201102BCLF_FPLC", //页面编码
      appcode: "201102BCLF", //小应用编码
    },
    (dataui) => {
      let metas = dataui.template;
      console.log("invoice metas:", dataui.template);
      let tempArr = [];
      let headmeta = metas[window.InvoiceWalletAreaCode.billsArea];
      let searchitem = metas[window.InvoiceWalletAreaCode.searchArea];

      let bxdata = window.invoiceListQueryParam;
      searchitem.items.forEach((item) => {
        if (item.itemtype == "refer") {
          item.isShowUnit = true;
        }
        if (item.attrcode && item.attrcode == "pk_org") {
          item.initialvalue = { value: table, display: table };
        }
        tempArr.push(item);
      });

      //票夹表头开票日期去掉hh:mm:ss
      headmeta.items.forEach(item => {
        if(item.attrcode === 'kprq') {
          item.itemtype = 'datepicker'
        }
      })

      //head add button
      addOperateButtons(headmeta, props);

      let meta = {
        ...metas,
        searchArea: {
          moduletype: "search",
          items: tempArr,
        },
        head: {
          moduletype: "table",
          items: headmeta.items,
        },
      };
      props.meta.setMeta(meta);
      let data = {
        querycondition: props.search.getAllSearchData(
          window.InvoiceWalletAreaCode.searchArea
        ),
        pagecode: window.invoicePageCode.searchPage,
        pageInfo: {
          pageIndex: -1,
          pageSize: 100,
          totalPage: "0",
        },
        queryAreaCode: window.InvoiceWalletAreaCode.searchArea, //查询区编码
        oid: meta[window.InvoiceWalletAreaCode.searchArea].oid, //pub_area 中PK_AREA
        querytype: "tree",
      };
      
      // 请求添加表体已关联发票的过滤
      let senddata = {
        bxdata,
        data,
        pagecode: window.presetVar.pagecode,
        tepmid: props.meta.getMeta().pageid,
      };
      const { getCardData } = this.props;
      const tableData = getCardData() || [];
      const selectedInvoiceList = [];
      for (let row of tableData) {
        if (row && row.values && row.values.pk_erminvoice) {
          selectedInvoiceList.push({
            pk_erminvoice: row.values.pk_erminvoice.value,
            generatetype: row.values.generatetype && row.values.generatetype.value,
          });
        }
      }
      senddata.selectedData = selectedInvoiceList;

      requestApi.query({
        data: senddata,
        success: (data) => {
          if (data) {
            //先取消勾选
            this.props.transferTable.cancelRowsChecked(
              "head",
              Object.keys(this.deleteList)
            );
            this.deleteList = {};

            this.setTransferBtnDisabled(false);
            data.forEach((item) => {
              if (!item.body) {
                item.body = { body: { areacode: "body", rows: [] } };
              }
            });

            this.invoiceList = data;
            data &&
              this.props.transferTable.setTransferTableValue(
                window.InvoiceWalletAreaCode.billsArea,
                window.InvoiceWalletAreaCode.bodysArea,
                data,
                "pk_erminvoice",
                "pk_erminvoice_b"
              );
          } else {
            // this.setTransferBtnDisabled(true);
            this.props.transferTable.setTransferTableValue(
              window.InvoiceWalletAreaCode.billsArea,
              window.InvoiceWalletAreaCode.bodysArea,
              [],
              "pk_erminvoice",
              "pk_erminvoice_b"
            );
          }
        },
      });
    }
  );
}

function addOperateButtons(headmeta, props) {
  const multiLang = props.MutiInit.getIntl(2011); //this.moduleId
  headmeta.items.push({
    attrcode: "custom_operate",
    color: "#555555",
    containlower: false,
    datatype: "51",
    disabled: false,
    itemtype: "customer",
    label: multiLang && multiLang.get('201102BCLF-0035'),  // "操作",
    visible: true,
    width: "100px",
    fixed: 'right',
    render: (text, record, index) => {
      return <a style={{cursor: 'pointer'}} onClick={() => {
        requestApi.showInvoiceImg({
          data: {pk_erminvoice: record.pk_erminvoice.value},
          success: (data) => {
            try {
              window.open(data.data[0].previewUrl)
            } catch (error) {
              toast({content: error.message, color: 'danger'})
            }
          }
        })
      }}>{multiLang && multiLang.get('201103INUP-0013')}</a>
  }
  })
}